package WordHandling;

import java.lang.Math.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

/**Represents a certain combination of the queried keywords and the files containing this combination of the words*/
public class File_Cluster /*implements ActionListener*/{

	/**x coordinate of the position of this circle in its frame.
	 * Actually the top left corner of the square surrounding this circle*/
	private int x_coord;
	/**y coordinate of the position of this circle in its frame.
	 * Actually the top left corner of the square surrounding this circle*/
	private int y_coord;
	/**radius used to determine position of this cluster in the frame*/
	private int radius;
	/**Tuple that this cluster represents. Contains a combination of keywords and corresponding result files*/
	visualization.tuple words_and_files;
	/**Graphical object used to display this cluster*/
	Graphics2D graph_obj;
	/**Circle icon which represents the cluster*/
	Shape circle;
	/**This */
	JButton rank_list = new JButton();
	/**Panel to attach this cluster to before attaching to the frame*/
	JPanel pane;
	/**Contains all clusters ever created while the program runs, used by action listener*/
	static ArrayList<File_Cluster> clusters = new ArrayList<File_Cluster>();
	/**Contains a master list of the buttons created to open the files, used by action listener*/
	static ArrayList<JButton> files = new ArrayList<JButton>();

	public File_Cluster(){}

	/**
	 * 
	 */
	public File_Cluster(Graphics g,JFrame f, visualization.tuple t,int x, int y, int r){
		x_coord=x;
		y_coord=y;
		radius=r;
		words_and_files=t;
		graph_obj = (Graphics2D)g;

		 circle = new Ellipse2D.Float(x, y, 25.0f, 25.0f);
		clusters.add(this);
	}
	
	/**Draws the circle representing the cluster and a number inside which tells the number of files that the cluster contains*/
	public void draw_file_cluster(){
		graph_obj.draw(circle);
	    graph_obj.setPaint(Color.green);
	    graph_obj.fill(circle);
	    
		graph_obj.setPaint(Color.black);
		graph_obj.drawString(String.valueOf(words_and_files.getFiles().size()),x_coord+(25/2),y_coord+(15));

	}
	
	/**@return the Number of files that this cluster contains as a String*/
	public String getNum(){return String.valueOf(words_and_files.getFiles().size());}
	
	/*//not used
	public void set_color(ArrayList<Line> lines){
		//divide circle into num_lines different sections, create a gradient for each fill circle with all gradients
		//x_start, y_start start at left edge of circle so xcenter+radius* cos(90 degrees) ycenter+radius* sin(90 degrees)
		double interval = (double)(radius*2)/lines.size();
		double x_start=x_coord+radius* Math.cos(90*(1/180));
		double y_start=y_coord+radius* Math.sin(90*(1/180))+radius;
		double x_end= x_start+interval;
		double y_end=y_start-radius*2;
		//well really it will need to be more like a point radius above this & end radius below this end_y end_x
		//then add interval to this point
	    // Red at (0,0), yellow at (175,175), changes gradually between.

	    for(int i=0; i<lines.size()-1;i++){
		GradientPaint gradient = new GradientPaint((int)x_start, (int)y_start, lines.get(i).getColor(), (int)x_end, (int)y_end,lines.get(i+1).getColor(), false); // true means to repeat pattern
	    graph_obj.setPaint(gradient);
	    graph_obj.fill(circle);//not sure if fill circle will mess this up
    	x_start = x_end;
    	y_start = y_end;
    	x_end += interval; 
    	x_end += interval;
    	
	    }
	//or we could use transparency ... probably a bit more complicated
	}*/
	
	/**
	 * @return list of keywords that this cluster represents
	 */
	public ArrayList<String> getWords(){
		return words_and_files.getWords();
	}
	
	/**
	 * Circle objects are drawn from squares so x_coord is upper left corner of square
	 * @return the x coordinate of the center of the circle
	 */
	public int getCenterX(){
		return x_coord+(25/2);
	}
	
	/**
	 * Circle objects are drawn from squares so y_coord is upper left corner of square
	 * @return the y coordinate of the center of the circle
	 */
	public int getCenterY(){
		return y_coord+(25/2);
	}
	
	/**
	 * @return the x coordinate for the circle (upper left corner of square)
	 */
	public int getX(){
		return x_coord;
	}
	
	/**
	 * @return the y coordinate for the circle (upper left corner of square)
	 */
	public int getY(){
		return y_coord;
	}
	/*
//not used
	public JPanel get_pane(){
		return pane;
	}*/
	/*
//not used???
	   public void actionPerformed(ActionEvent e) {
	    	Object o = e.getSource();
	       for(int i=0; i<clusters.size();i++){
	    	   if(o==clusters.get(i).rank_list){
	    		   JFrame ranked_files = new JFrame("ranked files");
	    		   //This will need to change ... just to get it to compile:
	    		   ArrayList<File> files_ = new ArrayList<File>();
	    		   files_= clusters.get(i).words_and_files.getFiles();
	    		   for(int j=0; j<files_.size();j++){
	    			   //make a new button for every file
	    			   JButton file = new JButton(files_.get(j).getName());//name it the name of the file
	    			   //Do I have to set name or does this work???
	    			   files.add(file);//placement???
	    		       JLabel file_info = new JLabel("Rank: ");//add more information here
	    		       JPanel pane = new JPanel();
	    			    SpringLayout layout = new SpringLayout();
	    		        layout.putConstraint(SpringLayout.WEST, file,5,SpringLayout.WEST, pane);
	    		        layout.putConstraint(SpringLayout.NORTH, file,10*j+10,SpringLayout.NORTH, pane);
	    		        layout.putConstraint(SpringLayout.WEST, file_info,5,SpringLayout.WEST, file);
	    		        layout.putConstraint(SpringLayout.NORTH, file_info,10*j+10,SpringLayout.NORTH, pane);
	    		        pane.setLayout(layout);//can I do this every time???
	    		        pane.add(file);
	    		        pane.add(file_info);
	    		        ranked_files.add(pane);
	    		        
	    		        file.addActionListener(new File_Cluster());

	                    ranked_files.setSize(500,500);//This may need to change ... Also may need to do something about the # files returned
	                    ranked_files.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	                    ranked_files.setVisible(true);
	    		       
	    		   }
	    	   }
	       }
	       for(int j=0;j<files.size();j++){
	    	   if(o == files.get(j))
	    	   {
	    		   JFrame open_file = new JFrame();
	    		   JTextArea file_text = new JTextArea();
	    		   String t="";
	    		   try{
	    		   File f= new File(files.get(j).getName()); //get File for the button pressed;
	    		   Scanner sc = new Scanner(f);
		           sc.useDelimiter("\\Z");
		           t = sc.next();
		           sc.close();
	    		   }
	    		   catch(FileNotFoundException ex){
	   	            ex.printStackTrace();
	   		   }
	    		   //literally open file and read into t using the scanner
	    		   file_text.setText(t);
	    		   
	    		   open_file.add(file_text);
                   open_file.setSize(500,500);//size???
                   open_file.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                   open_file.setVisible(true);
	    	   }
	       }
	   }
	    	
	    */
	
	
}
